include('shared.lua')

ENT.RenderGroup = RENDERGROUP_BOTH

local matLight 		= Material( "sprites/light_ignorez" )

function ENT:Initialize()
	self:UpdateNW()
	
	local min = self:OBBMins()
	self.PanelPos = self:OBBCenter()
	
	self.PanelPos.x = min.x + 17.5
	self.PanelPos.z = self.PanelPos.z + -7
	
	self.LightAttachment = self.PanelPos + Vector(0,0, 35.5)
	
	self.PixVis = util.GetPixelVisibleHandle()
	
end

function ENT:Think()
	
end

function ENT:DrawTranslucent()
	
	local pos = self:LocalToWorld( self.PanelPos )
	local ang = self:LocalToWorldAngles( Angle(0,90,90) )
	local Dif = 1 - self.Red - self.Blue

	cam.Start3D2D( pos, ang, 0.25 )
		
		// -15 -80
		// 30 160
		
		local BlueSize = math.ceil( self.Blue * 160 )
		local RedSize = math.ceil( self.Red * 160 )
		
		if Dif != 0 then
			surface.SetDrawColor( 0, 0, 0, 255 )
			surface.DrawRect( -15, -80 + BlueSize, 30, 160 - RedSize - BlueSize )
		end
		
		if self.Blue > 0 then	
			surface.SetDrawColor( 0, 0, 255, 255 )
			surface.DrawRect( -15, -80, 30, BlueSize )
		end
		
		if self.Red > 0 then
			surface.SetDrawColor( 255, 0, 0, 255 )
			surface.DrawRect( -15, -80 + 160 - RedSize, 30, RedSize )	
		end

	cam.End3D2D()
end

function ENT:GetLightColor( alpha )
	
	if self.Team == 2 then
		return Color( 255, 0, 0, alpha * 255 )
	elseif self.Team == 1 then
		return Color( 0, 0, 255, alpha * 255 )
	end

	return Color( 255, 255, 255, alpha * 155 )
end

function ENT:Draw()

	self:DrawModel()
	
	local LightPos = self:LocalToWorld( self.LightAttachment )
	local Visibile	= util.PixelVisible( LightPos, 4, self.PixVis )
	
	if Visibile then
		render.SetMaterial( matLight )
		render.DrawSprite( LightPos , 32, 32, self:GetLightColor( Visibile ) )
	end
	
	
end